﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using EmgrMvc.Models;
using EmgrMvc.BLL;
using EmgrMvc.ViewModels;

namespace EmgrMvc.Controllers
{
    public class DeviceGroupsController : Controller
    {
        private readonly MyContext _context;

        public DeviceGroupsController(MyContext context)
        {
            _context = context;
        }

        // GET: DeviceGroups
        public async Task<IActionResult> Index()
        {
            return View(await _context.Groups.ToListAsync());
        }

        // GET: DeviceGroups/Details/5
        public async Task<IActionResult> Details(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            DeviceGroupView viewModel = new DeviceGroupView();
            viewModel.group = await _context.Groups
                .FirstOrDefaultAsync(m => m.Id == id);
            if (viewModel.group == null)
            {
                return NotFound();
            }

            EsysBLL bll = new EsysBLL(_context);
            viewModel.groupDevices = bll.getDevicesByGroup(id.Value);
            ViewBag.AllDevices = new SelectList(bll.getDevices(), "Id", "Name");

            return View(viewModel);
        }

        [HttpPost]
        public IActionResult AddDevice([FromForm]int id, [FromForm]int DeviceWillAdd)
        {
            EsysBLL esysBLL = new EsysBLL(_context);
            esysBLL.AddGroupDevice(id, DeviceWillAdd);
            return RedirectToAction("Details", new { Id = id});
        }


        public IActionResult RemoveDevice(int id, int DeviceId)
        {
            EsysBLL esysBLL = new EsysBLL(_context);
            esysBLL.RemoveGroupDevice(id, DeviceId);
            return RedirectToAction("Details", new { Id = id });
        }

        // GET: DeviceGroups/Create
        public IActionResult Create()
        {
            return View();
        }

        // POST: DeviceGroups/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("Id,Name,Description")] DeviceGroup deviceGroup)
        {
            if (ModelState.IsValid)
            {
                _context.Add(deviceGroup);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            return View(deviceGroup);
        }

        // GET: DeviceGroups/Edit/5
        public async Task<IActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var deviceGroup = await _context.Groups.FindAsync(id);
            if (deviceGroup == null)
            {
                return NotFound();
            }
            return View(deviceGroup);
        }

        // POST: DeviceGroups/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(int id, [Bind("Id,Name,Description")] DeviceGroup deviceGroup)
        {
            if (id != deviceGroup.Id)
            {
                return NotFound();
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(deviceGroup);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!DeviceGroupExists(deviceGroup.Id))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
                return RedirectToAction(nameof(Index));
            }
            return View(deviceGroup);
        }

        // GET: DeviceGroups/Delete/5
        public async Task<IActionResult> Delete(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var deviceGroup = await _context.Groups
                .FirstOrDefaultAsync(m => m.Id == id);
            if (deviceGroup == null)
            {
                return NotFound();
            }

            return View(deviceGroup);
        }

        // POST: DeviceGroups/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> DeleteConfirmed(int id)
        {
            var deviceGroup = await _context.Groups.FindAsync(id);
            _context.Groups.Remove(deviceGroup);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }

        [HttpGet]
        public IActionResult PowerOn(int id)
        {
            EsysBLL bll = new EsysBLL(_context);
            bll.PowerOnByGroup(id);
            return RedirectToAction(nameof(Index));
        }

        [HttpGet]
        public IActionResult PowerOff(int id)
        {
            EsysBLL bll = new EsysBLL(_context);
            bll.PowerOffByGroup(id);
            return RedirectToAction(nameof(Index));
        }

        private bool DeviceGroupExists(int id)
        {
            return _context.Groups.Any(e => e.Id == id);
        }
    }
}
